<?php
/** class.staticcontent.php
 * 
 * Cache static content
 * 
 * @author Ted, tass2001-at-gmail-dot-com
 * @package phpsimpleblog
 * @version $Id$
 * @website https://code.google.com/p/phpsimpleblog/
 */
class StaticContent extends BlogBase{
    
    public function __construct(){
	parent::__construct();
	$this->site_data = array();
	$this->xml_dom = new DOMDocument();
        $this->xml_dom->encoding = 'UTF-8';
	$this->__cacheData();
    }
    
    private function __cacheData(){
	$query = $this->db->query(sprintf("SELECT ID,title,UNIX_TIMESTAMP(last_modified) as lastmod_ts,last_modified as lastmod, DATE_FORMAT(date,'%%b %%d, %%Y') as date
					  FROM %s
					  WHERE hidden='0' AND special='0' ORDER BY date DESC",ENTRY_TABLE));
	while($data = $query->fetch_assoc())
	    $this->site_data[] = $data;
    }
    
    private function __calculateMetrics($modified){
        $age = time() - $modified;
        $day = 86400;
        $properties = array();
        if($age < $day){
            $properties = array('changefreq' => 'daily','priority' => (float)1.0);
        }elseif($age > $day &&  $age < $day*7){
            $properties = array('changefreq' => 'weekly','priority' => (float)0.8);
        }elseif($age >= $day*7 &&  $age < $day*31){
            $properties = array('changefreq' => 'monthly','priority' => (float)0.4);
        }elseif($age >= $day*31){
            $properties = array('changefreq' => 'yearly','priority' => (float)0.2);
        }
        return $properties;
    }
    
    private function __createDOMElementWithText($tag_name,$text){
        $new_element = $this->xml_dom->createElement($tag_name);
        $new_text = $this->xml_dom->createTextNode($text);
        $new_element->appendChild($new_text);
        return $new_element;
    }
    
    private function __createUrl($data){
        $url_el = $this->xml_dom->createElement('url');
        foreach($data as $tag=>$val)
            $url_el->appendChild($this->__createDOMElementWithText($tag,$val));
        return $url_el;
    }
    
    public function cacheArchives(){
	$json_data = array();
	foreach($this->site_data as $post_item)
	    $json_data[] = array('id'=> $post_item['ID'],'title' => $post_item['title'],'date' => $post_item['date']);
	return file_put_contents(DOC_ROOT.$this->config->static_content->archives,json_encode($json_data));
    }
    
    public function cacheSidebar(){
	$json_data = array();
	$i = 0;
	for($i = 0; $i < 5 && $i < count($this->site_data); $i++){
	    $json_data[] = array('title' => $this->site_data[$i]['title'],'href' => sprintf('/post/%s',$this->site_data[$i]['ID']));
	}
	return file_put_contents(DOC_ROOT.$this->config->static_content->sidebar,json_encode($json_data));
    }
    
    public function cacheSitemap(){
        $this->base_element = $this->xml_dom->createElement('urlset');
        $this->base_element->setAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
	$max_mod_time = 0;
        foreach($this->site_data as $entry){
	    $max_mod_time = ($entry['lastmod_ts'] > $max_mod_time) ? $entry['lastmod_ts'] : $max_mod_time;
            $metrics = $this->__calculateMetrics($entry['lastmod_ts']);
            $url_data = array('loc' => sprintf('%s/post/%s',$this->config->http_host,$entry['ID']),'lastmod' => $entry['lastmod'],
                              'changefreq' => $metrics['changefreq'],'priority' => $metrics['priority']);
            $this->base_element->appendChild($this->__createUrl($url_data));
        }
	$home_metrics = $this->__calculateMetrics($max_mod_time);
        $home_data = array('loc' => $this->config->http_host,'lastmod'=>date('Y-m-d H:i:s',$max_mod_time),'changefreq'=>$home_metrics['changefreq'],'priority'=>1.0);
        $this->base_element->appendChild($this->__createUrl($home_data));
        $this->xml_dom->appendChild($this->base_element);
        return $this->xml_dom->save(DOC_ROOT.$this->config->static_content->sitemap);
    }
    
}
?>